﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic; 

namespace Runda.CMS.Portal.StockManagement
{
    public partial class AccessoryReplacementApprove : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        public Guid ReplacementID
        {
            get { return new Guid(Request.QueryString["ReplacementID"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBelongto.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.AccessoryReplacementBelongto)));

                MasterDataBll bll = new MasterDataBll();
                List<SysDictionaryEntity> list = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.AccessoryPaymentsObject);
                ddlPaymentsObject.DataSource = list;
                ddlPaymentsObject.DataTextField = "Name";
                ddlPaymentsObject.DataValueField = "ID";
                ddlPaymentsObject.DataBind();

                list = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.AccessoryPaymentType);
                ddlPaymentType.DataSource = list;
                ddlPaymentType.DataTextField = "Name";
                ddlPaymentType.DataValueField = "ID";
                ddlPaymentType.DataBind();


                list = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.AccessorySource);
                ddlAccessoryResource.DataSource = list;
                ddlAccessoryResource.DataTextField = "Name";
                ddlAccessoryResource.DataValueField = "ID";
                ddlAccessoryResource.DataBind();

                WsmAccessoryReplacementEntity entity = bll.Get<WsmAccessoryReplacementEntity>(ReplacementID);
                if (entity != null)
                {
                    hiddenMaintainID.Value = entity.MaintainID.ToString();
                    WsmMaintainBillEntity mentity = bll.Get<WsmMaintainBillEntity>(entity.MaintainID);
                    if(mentity != null)
                        hiddenRegisterID.Value = mentity.RegisterID.ToString();

                    txtAccessaryID.Value = entity.AccessoryID.ToString();
                    txtAccssoryCode.Text = entity.AccssoryCode;
                    txtAccessaryName.Text = entity.AccessoryName;
                    txtStock.Text = entity.PersonalStockQuantity.ToString();
                    txtAccessoryVendor.Text = "";
                    txtReplacementNum.Text = entity.ReplacementNum.ToString();
                    txtQuotePrice.Text = entity.QuotePrice.ToString();

                    ddlBelongto.SelectedValue = entity.ReplacementBelongto.ToString();
                    ddlAccessoryPaymentStatus.SelectedValue = entity.AccessoryPaymentStatus.ToString();

                    ddlPaymentsObject.SelectedValue = entity.PaymentsObject.ToString();
                    txtPaymentsObjectDesc.Text = entity.PaymentsObjectDesc;
                    ddlPaymentType.SelectedValue = entity.PaymentType.ToString();
                    ddlAccessoryResource.SelectedValue = entity.AccessoryResource.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MaintainBillBll bll = new MaintainBillBll();

            WsmAccessoryReplacementEntity entity = bll.Get<WsmAccessoryReplacementEntity>(ReplacementID);
            if (entity != null)
            {
                entity.CheckMan = CurrentUserID;
                entity.CheckDate = DateTime.Now;
                entity.CheckStatus = (int)Enumerator.AccessoryReplacementCheckStatus.Approved;
 
                entity.QuotePrice = string.IsNullOrEmpty(txtQuotePrice.Text.Trim()) ? 0 : Convert.ToDecimal(txtQuotePrice.Text.Trim());
                entity.BillOrderStatus = 1;//订单状态 0 未下订单 1 已下订单 2 入库
                entity.ReplacementNum = string.IsNullOrEmpty(txtReplacementNum.Text.Trim()) ? 0 : Convert.ToInt32(Convert.ToDecimal(txtReplacementNum.Text.Trim()));
                entity.ReplacementBelongto = Convert.ToInt32(ddlBelongto.SelectedValue);
                if (ddlAccessoryResource.SelectedValue != "")
                    entity.AccessoryResource = new Guid(ddlAccessoryResource.SelectedValue);//配件来源
                entity.AccessoryName = txtAccessaryName.Text;
                entity.AccessoryPaymentStatus = Convert.ToInt32(ddlAccessoryPaymentStatus.SelectedValue);
          
                if (ddlPaymentsObject.SelectedValue != "")
                    entity.PaymentsObject = new Guid(ddlPaymentsObject.SelectedValue);
                if (ddlPaymentType.SelectedValue != "")
                    entity.PaymentType = new Guid(ddlPaymentType.SelectedValue);

                /////entity.StockID = "";
                entity.BillNum = "";
                entity.Deleted = false;
                entity.AccessoryID = new Guid(txtAccessaryID.Value);
                entity.PaymentsObjectDesc = txtPaymentsObjectDesc.Text;

                entity.PersonalStockQuantity = int.Parse(txtStock.Text);
                entity.AccssoryCode = txtAccssoryCode.Text;

                entity.Remark = txtRemark.Text; //下定备注

                bll.UpdateMaintainBillCheckStatus(entity,CurrentUserID,CurrentUserEntity.UserName);
                ClientScript.RegisterStartupScript(this.GetType(), "success", "SaveAndCloseWindow();", true);
            }
        }
    }
}
